package controll;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

import model.InfoListener;
import model.Snake;
import view.About;
import view.GamePanel;
import view.InfoPanel;

public class MainFrame extends JFrame implements ActionListener,InfoListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GamePanel gamePanel;
	private InfoPanel infoPanel;
	private About about;
	private Snake snake;

	Font fon = new Font("Microsoft Yahei", Font.BOLD, 15);
	Font font = new Font("Microsoft Yahei", Font.PLAIN, 13);

	//主界面
	public MainFrame() {
		setVisible(true);
		setSize(1100, 860);
		setTitle("贪吃蛇");
		setLocation(400, 150);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(null);                 

   //菜单栏
		String[] menus = { "游戏", "难度", "帮助" };
		String[][] items = { { "新游戏", "暂停", "继续", "退出" }, { "慢速", "中速", "快速" }, { "关于" } };
		JMenuBar mb = new JMenuBar();
		for (int i = 0; i < menus.length; i++) {
			JMenu m = new JMenu(menus[i]);
			for (int j = 0; j < items[i].length; j++) {
				JMenuItem item = new JMenuItem(items[i][j]);
				m.add(item);
				m.setFont(fon);
				item.addActionListener(this);
				item.setFont(font);
			}
			mb.add(m);

		}
		setJMenuBar(mb);
		setLayout(null);
		setResizable(false);   //界面不可更改
		snake = new Snake();
		snake.openMoveThread(this);
		gamePanel = new GamePanel();
		gamePanel.setLocation(0, 0);
		add(gamePanel);
		
		
		infoPanel = new InfoPanel(snake);
		infoPanel.setLocation(GamePanel.WIDTH,0);
		infoPanel.showState();
		add(infoPanel);
		
		about=new About(this,true); 
		about.setSize(300, 300);
		about.setLocation(500,200);
        about.setTitle("关于");
		
		addKeyListener(new KeyAdapter() { // 设置键盘监听 （添加一个适配器）

			@Override
			public void keyPressed(KeyEvent e) { // 重写keypressed（一个按键按下事件e） 方法
				// TODO Auto-generated method stub
				super.keyPressed(e); // 启用keypressed方法

				switch (e.getKeyCode()) { // （键盘编码）键盘按下的键
				case KeyEvent.VK_UP: // VK_键盘的按键
					snake.changeDirection(Snake.UP);
					break;
				case KeyEvent.VK_RIGHT:
					snake.changeDirection(Snake.RIGHT);
					break;
				case KeyEvent.VK_DOWN:
					snake.changeDirection(Snake.DOWN);
					break;
				case KeyEvent.VK_LEFT:
					snake.changeDirection(Snake.LEFT);
					break;

				}

			}

		});

	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {

				new MainFrame();

			}
		});

	}

	public void actionPerformed(ActionEvent e) {

		String cmd = e.getActionCommand();

		switch (cmd) {

		case "新游戏":

			snake.addObserver(gamePanel);

			snake.init();
			
            infoPanel.showState();
            
			break;
			

		case "暂停":

			snake.addObserver(gamePanel);
			
		    snake.run_state=false;
		   
	        infoPanel.showState();
	            
		    break;
			

		case "继续":

			snake.addObserver(gamePanel);
			
		    snake.run_state=true;
		   
	        infoPanel.showState();
	            
		    break;
		    
		case "退出":

			System.exit(0);
	     
		    break;
		 
		case "慢速":

			snake.addObserver(gamePanel);
			
		    snake.speed=500;
		   
	        infoPanel.showState();
	         
	        InfoPanel.label[2].setText("慢速");
	        
		    break;
		case "中速":

			snake.addObserver(gamePanel);
			
		    snake.speed=200;
		   
		    InfoPanel.label[2].setText("中速");
		    
	        infoPanel.showState();
	            
		    break;
		case "快速":

			snake.addObserver(gamePanel);
			
		    snake.speed=50;
		    
		    InfoPanel.label[2].setText("快速");
		   
	        infoPanel.showState();
	            
		    break;
		    
		    
		case "关于":
	          about.setVisible(true);
		    break; 
		    
		}
		
	}

	@Override
	public void infoChange() {
		infoPanel.showState();
	}

}
